%run this file separately for each msa
pull=4;

%case shiller index-logged
%alphabetical order
csdata=importdata('cs.txt');cstemp=csdata(:,pull);csid=csdata(:,1);
%this will vary by city depending on how many cs are available
csfull=cstemp;

data=importdata('dc_salepr_close.txt');
dayl=data(:,1);my=data(:,2);my0=data(:,3);salepr=data(:,4);price=data(:,5);rw=data(:,7);rw2=data(:,8);id=data(:,10);

myt=unique(my);
N=length(dayl);
for i=1:N    
    my(i)=find(my(i)==myt);
end

fulldata=[dayl my my0 salepr price rw rw2 id ];

% replace each observation with 2 copies to replicate CS methodology
fulldata=repmat(fulldata,3,1);
fulldata=sortrows(fulldata,8);
dayl=fulldata(:,1);my=fulldata(:,2);my0=fulldata(:,3);salepr=fulldata(:,4);price=fulldata(:,5);rw=fulldata(:,6);rw2=fulldata(:,7);
id=fulldata(:,8);
N=length(id);
for i=3:N
    if(id(i)==id(i-1)&&id(i)~=id(i-2))
        my(i)=my(i)+1;my0(i)=my0(i)+1;
    elseif(id(i)==id(i-1)&&id(i)==id(i-2))
        my(i)=my(i)+2;my0(i)=my0(i)+2;
    end
end
my(2)=my(2)+1;my0(2)=my0(2)+1;

delta0=csfull(my0);
yv=(salepr./exp(delta0)).*rw.*rw2;

%form the X and Z matrices in case shiller; 
X=zeros(N,length(unique(my)));Z=X;
for i=1:size(Z,2)
    X(:,i)=price.*rw.*rw2.*(my==i);
    Z(:,i)=rw.*rw2.*(my==i);
end

A=Z'*X;B=Z'*yv;
coeff=(1./(A\B));


out=[myt log(coeff(1:length(unique(myt))))];
save dcforesale.txt out -ascii -double -tabs 
